package com.ssh.admin.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionSupport;
import com.ssh.common.log.LogFactory;
import com.ssh.common.log.LogName;
import com.ssh.common.util.JsonResult;
import com.ssh.common.util.Page;
import com.ssh.entity.MarketAnalysis;
import com.ssh.entity.Product;
import com.ssh.entity.SuperMarket;
import com.ssh.manager.MarketAnalysisManager;
import com.ssh.manager.ProductManager;
import com.ssh.manager.SuperMarketManager;

@Controller
@ParentPackage("admin")
@Scope("prototype")
@Results(value={
@Result(name="success", location="/admin/marketAnalysis/marketAnalysis.jsp"),
@Result(name="findList", type="json", params={"root","page","excludeproperties","page\\.curPageNum,page\\.totalPageNum,page\\.totalCount," +
		"page\\.numberPerPage,page\\.pageData\\[\\d+\\]\\.id,page\\.pageData\\[\\d+\\]\\.targetPrice,page\\.pageData\\[\\d+\\]\\.price," +
		"page\\.pageData\\[\\d+\\]\\.targetChenlie,page\\.pageData\\[\\d+\\]\\.chenlie,page\\.pageData\\[\\d+\\]\\.targetHuodong," +
		"page\\.pageData\\[\\d+\\]\\.huodong,page\\.pageData\\[\\d+\\]\\.targetHaibao,page\\.pageData\\[\\d+\\]\\.haibao," +
		"page\\.pageData\\[\\d+\\]\\.targetDuitou,page\\.pageData\\[\\d+\\]\\.duitou,page\\.pageData\\[\\d+\\]\\.targetZhuangui," +
		"page\\.pageData\\[\\d+\\]\\.zhuangui,page\\.pageData\\[\\d+\\]\\.time,page\\.pageData\\[\\d+\\]\\.superMarket\\.id," +
		"page\\.pageData\\[\\d+\\]\\.superMarket\\.name,page\\.pageData\\[\\d+\\]\\.superMarket\\.company\\.id,page\\.pageData\\[\\d+\\]\\.superMarket\\.company\\.name," +
		"page\\.pageData\\[\\d+\\]\\.product\\.id,page\\.pageData\\[\\d+\\]\\.product\\.name,page\\.pageData\\[\\d+\\]\\.product\\.brand\\.id," +
		"page\\.pageData\\[\\d+\\]\\.product\\.brand\\.name"}),
@Result(name="findAll", type="json", params={"root","jr","excludeproperties","data\\[\\d+\\]\\.id, data\\[\\d+\\]\\.targetPrice," +
		"data\\[\\d+\\]\\.price, data\\[\\d+\\]\\.targetChenlie, data\\[\\d+\\]\\.chenlie, data\\[\\d+\\]\\.targetHuodong," +
		"data\\[\\d+\\]\\.huodong, data\\[\\d+\\]\\.targetHaibao,data\\[\\d+\\]\\.haibao," +
		"data\\[\\d+\\]\\.targetDuitou,data\\[\\d+\\]\\.duitou,data\\[\\d+\\]\\.targetZhuangui," +
		"data\\[\\d+\\]\\.zhuangui,data\\[\\d+\\]\\.time,data\\[\\d+\\]\\.superMarket\\.id," +
		"data\\[\\d+\\]\\.superMarket\\.name,data\\[\\d+\\]\\.superMarket\\.company\\.id,data\\[\\d+\\]\\.superMarket\\.company\\.name," +
		"data\\[\\d+\\]\\.product\\.id,data\\[\\d+\\]\\.product\\.name,data\\[\\d+\\]\\.product\\.brand\\.id," +
		"data\\[\\d+\\]\\.product\\.brand\\.name"}),
/*@Result(name="findBy", type="json", params={"root","jr","excludeproperties","data\\[\\d+\\]\\.id, data\\[\\d+\\]\\.targetChenlie, data\\[\\d+\\]\\.targetDuitou" +
		", data\\[\\d+\\]\\.targetHaibao, data\\[\\d+\\]\\.targetHuodong, data\\[\\d+\\]\\.targetZhuangui, data\\[\\d+\\]\\.chenlie, data\\[\\d+\\]\\.duitou" +
		", data\\[\\d+\\]\\.haibao, data\\[\\d+\\]\\.huodong, data\\[\\d+\\]\\.zhuangui, data\\[\\d+\\]\\.productId, data\\[\\d+\\]\\.productName" +
		", data\\[\\d+\\]\\.brandId, data\\[\\d+\\]\\.brandName, data\\[\\d+\\]\\.marketId, data\\[\\d+\\]\\.marketName, data\\[\\d+\\]\\.marketContent" +
		", data\\[\\d+\\]\\.companyId, data\\[\\d+\\]\\.companyName, data\\[\\d+\\]\\.visitTime"}),*/
@Result(name="findBy", type="json", params={"root","jr"}),
@Result(name="delete", type="json", params={"root","page","excludeproperties","page\\.curPageNum"})
})
public class MarketAnalysisAction extends ActionSupport{
	@Resource(name="marketAnalysisManagerImpl")
	private MarketAnalysisManager marketAnalysisManager;
	@Resource(name="superMarketManagerImpl")
	private SuperMarketManager superMarketManager;
	@Resource(name="productManagerImpl")
	private ProductManager productManager;
	
	private Page page = new Page();
	private JsonResult jr=new JsonResult();
	private String isDesc;
	private MarketAnalysis marketAnalysis;
	private List<MarketAnalysis> mAnalysis;
	private String ids;
	private SuperMarket superMarket;
	private Product product;
	private int orderBy;
	private List<Map<String, String>> arrayList;
	/**
	 * 1>月同比、2>月环比、3>年环比
	 */
	private int timeType;
	
	public List<Map<String, String>> getArrayList() {
		return arrayList;
	}

	public void setArrayList(List<Map<String, String>> arrayList) {
		this.arrayList = arrayList;
	}

	public int getTimeType() {
		return timeType;
	}

	public void setTimeType(int timeType) {
		this.timeType = timeType;
	}

	public int getOrderBy() {
		return orderBy;
	}

	public void setOrderBy(int orderBy) {
		this.orderBy = orderBy;
	}

	public String goJsp(){
		return "success";
	}
	
	public String findBy(){
		try {
			mAnalysis = new ArrayList<MarketAnalysis>();
			List<MarketAnalysis> mList = marketAnalysisManager.findByCondition(timeType, orderBy);
			List<MarketAnalysis> mList2 = new ArrayList<MarketAnalysis>();
			for (int i = 0; i < mList.size(); i++) {
				if (i==0) {
					mList2.add(mList.get(i));
				}else {
					int t = 0;
					for (int j = 0; j < mList2.size(); j++) {
						if(orderBy==1){
							if (mList2.get(j).getProduct().getId()==mList.get(i).getProduct().getId()) {
								t = 1;
							}
						}else {
							if (mList2.get(j).getSuperMarket().getId()==mList.get(i).getSuperMarket().getId()) {
								t = 1;
							}
						}
					}
					if (t==0) {
						mList2.add(mList.get(i));
					}
				}
			}
			arrayList = new ArrayList<Map<String,String>>();
			for (int i = 0; i < mList2.size(); i++) {
				MarketAnalysis m = mList2.get(i);
				for (int j = 0; j < mList.size(); j++) {
					int k = 0;
					if(orderBy==1){
						if (m.getProduct().getId()==mList.get(j).getProduct().getId()) {
							k = 1;
						}
					}else {
						if (m.getSuperMarket().getId()==mList.get(j).getSuperMarket().getId()) {
							k = 1;
						}
					}
					if (k==1 && m.getId()!=mList.get(j).getId()) {
						m.setTargetChenlie(m.getTargetChenlie()+mList.get(j).getTargetChenlie());
						m.setTargetDuitou(m.getTargetDuitou()+mList.get(j).getTargetDuitou());
						m.setTargetHaibao(m.getTargetHaibao()+mList.get(j).getTargetHaibao());
						m.setTargetHuodong(m.getTargetHuodong()+mList.get(j).getTargetHuodong());
						m.setTargetZhuangui(m.getTargetZhuangui()+mList.get(j).getTargetZhuangui());
						m.setChenlie(m.getChenlie()+mList.get(j).getChenlie());
						m.setHaibao(m.getHaibao()+mList.get(j).getHaibao());
						m.setDuitou(m.getDuitou()+mList.get(j).getDuitou());
						m.setHuodong(m.getHuodong()+mList.get(j).getHuodong());
						m.setZhuangui(m.getZhuangui()+mList.get(j).getZhuangui());
					}
				}
				Map<String, String> map = new HashMap<String, String>();
				map.put("id", m.getId().toString());
				map.put("targetChenlie", m.getTargetChenlie()+"");
				map.put("targetDuitou", m.getTargetDuitou()+"");
				map.put("targetHaibao", m.getTargetHaibao()+"");
				map.put("targetHuodong", m.getTargetHuodong()+"");
				map.put("targetZhuangui", m.getTargetZhuangui()+"");
				map.put("chenlie", m.getChenlie()+"");
				map.put("duitou", m.getDuitou()+"");
				map.put("huodong", m.getHuodong()+"");
				map.put("haibao", m.getHaibao()+"");
				map.put("zhuangui", m.getZhuangui()+"");
				map.put("productId", m.getProduct().getId().toString());
				map.put("productName", m.getProduct().getName());
				map.put("brandId", m.getProduct().getBrand().getId().toString());
				map.put("brandName", m.getProduct().getBrand().getName());
				map.put("marketId", m.getSuperMarket().getId().toString());
				map.put("marketName", m.getSuperMarket().getName());
				map.put("marketContent", m.getSuperMarket().getContent());
				map.put("companyId", m.getSuperMarket().getCompany().getId().toString());
				map.put("companyName", m.getSuperMarket().getName());
				map.put("visitTime", m.getTime().toString());
				System.out.println(map.get("targetChenlie"));
				arrayList.add(map);
				//mAnalysis.add(m);
			}
			jr.setCode("0");
			jr.setData(arrayList);
		} catch (Exception e) {
			jr.setCode("-1");
			jr.setMessage(e.getMessage());
			LogFactory.getDefaultLog().error("查询失败", LogName.API);
		}
		return "findBy";
	}
	
	public String findAll(){
		mAnalysis = marketAnalysisManager.findAll();
		return "findAll";
	}
	
	public String findList(){
		Map conditionMap = new HashMap();
		marketAnalysisManager.findList(conditionMap, page, isDesc);
		return "findList";
	}
	
	public String add(){
		superMarket = superMarketManager.getSuperMarketById(superMarket.getId());
		product = productManager.getProductById(product.getId());
		marketAnalysis.setSuperMarket(superMarket);
		marketAnalysis.setProduct(product);
		marketAnalysis.setDel(0);
		try {
			marketAnalysisManager.saveUpdate(marketAnalysis);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "success";
	}
	
	public String update(){
		
		return "";
	}
	
	public String delete(){
		try {
			if(ids!=null && !ids.equals("")){
				String[] d = ids.split(",");
				for (int i = 0; i < d.length; i++) {
					marketAnalysis = marketAnalysisManager.getById(new Long(d[i]));
					marketAnalysis.setDel(1);
					marketAnalysisManager.saveUpdate(marketAnalysis);
				}
			}else {
				marketAnalysis = marketAnalysisManager.getById(marketAnalysis.getId());
				marketAnalysis.setDel(1);
				marketAnalysisManager.saveUpdate(marketAnalysis);
			}
			int _p = (int) page.getCurPageNum();
			page.setNumberPerPage(20);
			page.setCurPageNum(1);
			Map conditionMap = new HashMap();
			marketAnalysisManager.findList(conditionMap, page, isDesc);
			if(_p<=page.getTotalPageNum()){
				page.setCurPageNum(_p);
			}else{
				page.setCurPageNum(_p-1);
			}
			jr.setCode("y");
		} catch (Exception e) {
			e.printStackTrace();
			jr.setCode("n");
		}
		return "delete";
	}
	
	public List<MarketAnalysis> getmAnalysis() {
		return mAnalysis;
	}

	public void setmAnalysis(List<MarketAnalysis> mAnalysis) {
		this.mAnalysis = mAnalysis;
	}

	public String getIds() {
		return ids;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}

	public Page getPage() {
		return page;
	}
	public void setPage(Page page) {
		this.page = page;
	}
	public JsonResult getJr() {
		return jr;
	}
	public void setJr(JsonResult jr) {
		this.jr = jr;
	}
	public String getIsDesc() {
		return isDesc;
	}
	public void setIsDesc(String isDesc) {
		this.isDesc = isDesc;
	}
	public MarketAnalysis getMarketAnalysis() {
		return marketAnalysis;
	}
	public void setMarketAnalysis(MarketAnalysis marketAnalysis) {
		this.marketAnalysis = marketAnalysis;
	}

	public SuperMarket getSuperMarket() {
		return superMarket;
	}

	public void setSuperMarket(SuperMarket superMarket) {
		this.superMarket = superMarket;
	}

	public Product getProduct() {
		return product;
	}

	public void setProduct(Product product) {
		this.product = product;
	}
}
